package com.angus.modules.content.repository;

import com.angus.common.core.utils.StringUtils;
import com.angus.modules.content.domain.Course;
import com.angus.modules.content.mapper.CourseMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liyanan
 * @date 2024/10/21 10:16 AM
 * @description
 */
@Repository
public class CourseRepository extends ServiceImpl<CourseMapper, Course> {


    public List<Course> getCourseByType(String type) {
        LambdaQueryWrapper<Course> queryMapper = Wrappers.lambdaQuery(Course.class);
        queryMapper.eq(Course::getStatus,1).eq(Course::getDelFlag,0);
        if (StringUtils.isNotBlank(type)){
            queryMapper.eq(Course::getContentType,type);
        }
        return baseMapper.selectList(queryMapper);
    }

    public Course getCourseByCode(String courseCode) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(Course.class)
                .eq(Course::getCourseCode,courseCode)
                .eq(Course::getDelFlag,0));
    }

    public Course getCourseByName(String courseName) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(Course.class)
                .eq(Course::getCourseName,courseName)
                .eq(Course::getDelFlag,0)
                .last("limit 1"));
    }

    public boolean updateStatus(Long id, Integer value) {
        int update = baseMapper.update(null, Wrappers.lambdaUpdate(Course.class)
                .set(Course::getStatus, value)
                .eq(Course::getId, id)
                .eq(Course::getDelFlag,0));
        return update > 0;
    }

    public List<Long> listByIds(List<Long> ids) {
        List<Course> idList = baseMapper.selectList(Wrappers.lambdaQuery(Course.class)
                .in(Course::getId, ids)
                .eq(Course::getDelFlag,0));
        return idList.stream().map(Course::getId).collect(Collectors.toList());
    }

    public boolean deleteByIds(List<Long> ids){
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
